import 'package:dio/dio.dart';
import 'package:cream_chat_flutter/configs/Apis.dart';
import 'package:cream_chat_flutter/configs/Host.dart';

class _CustomInterceptors extends Interceptor {
  @override
  void onRequest(RequestOptions options, RequestInterceptorHandler handler) {
    print('REQUEST[${options.method}] => PATH: ${options.path}');
    handler.next(options);
  }

  @override
  void onResponse(Response response, ResponseInterceptorHandler handler) {
    if (response == null || response.data == null) {
      print("${response.realUri.path}:响应结果为空");
      handler.next(null);
      return;
    } else if (response.statusCode == 401 || response.data["code"] == 401) {
      print("去登录页面");
      handler.next(null);
      return;
    } else if (response.statusCode != 200 || response.data["code"] != 200) {
      print("${response.realUri.path}:响应失败:${response.data}");
      handler.next(null);
      return;
    }
    handler.next(response);
  }

  @override
  void onError(DioError err, ErrorInterceptorHandler handler) {
    print(err);
    handler.reject(err);
  }
}

final Interceptor _defaultInterceptor = _CustomInterceptors();

Map<String, dynamic> _getHeaders(Map<String, dynamic> headers) {
  if (headers == null) {
    headers = {};
  }
  headers["token"] = "5KWUdJQxYq1z4i/1Y9aqNdPAbiY5JWFtg+jGYd1c1Y5cMxHhbdOu5Mq+FlfP2Cqwoff2ovYvpQxv4niI4P9pl65L48xelEz+PnhFTiUnqA+Y8C2Sd8bj7fGvvBDgmIehx6bEgD+Ilo4oSO3gHMSdH7boAwnoc9sdpdme9tISR9OlycNvqdhWYxS7X/SsY4np/3flKkhv23AAvMqloAj0M/Ybds9M9eoI8mnJOH5M+Uv2xg0alfB/+wZCLCL6QvsuqoFBW4QOACh1DapUFM4ZIGEAvVKR+FYeKBCUDTDxg/TRKLcx73LNnWPJED7iZ+0Hq9NPjoae+k1Okdn4Oy0px4IhCQ1WRSn1Eh2ELlnSP/aAsxqV36WIMibvyInwdGviSdlkvyfUqOUJNmywmg+tlIbGpBrovuO57QaFuDh70bXJzIm0SEbLb7q99yNtvSnCCKOovky0bNDC9a+OQ/Z5DivfZIE7n1L5SaUAiioTDLnuL85yF6Ak1y5lsGam5bjnHUJ1ca9sciRW5eFEt6dfN7nxqSFrEOSCXlsj+LDpj3l4zpvGsDjcriHiRLggZRC/A9QalXgqtKUppzJ0x7DHmNnNdaNira39QH57zFHsSvPnj3smNsheF6zhFJVe8N5cpgrqV0x2pOsK7Lopc+FwD5P0EkJb+8rCnXCm57qWZxhpKAdXLd8+N9JHtM6vNfOytjhCNqxBtcOjMyuOR93A7Q==";
  return headers;
}

Dio _dio;

Future<dynamic> _request(Api api,
    {Map<String, dynamic> queryParams,
    dynamic data,
    Map<String, dynamic> headers,
    Interceptor interceptor}) async {
  _dio = Dio();
  Response response;
  headers = _getHeaders(headers);
  List<Interceptor> interceptors = [_defaultInterceptor];
  if (interceptor != null) {
    interceptors.add(interceptor);
  }
  _dio.interceptors.addAll(interceptors);
  switch (api.method) {
    case GET:
      response = await _dio.get(host + api.url,
          queryParameters: queryParams, options: Options(headers: headers));
      break;
    case POST:
      response = await _dio.post(host + api.url,
          queryParameters: queryParams,
          data: data,
          options: Options(
              headers: headers, contentType: "application/json;charset=UTF-8"));
      break;
    case PUT:
      response = await _dio.put(host + api.url,
          queryParameters: queryParams,
          data: data,
          options: Options(
              headers: headers, contentType: "application/json;charset=UTF-8"));
      break;
    case DELETE:
      response = await _dio.delete(host + api.url,
          queryParameters: queryParams, options: Options(headers: headers));
      break;
    default:
      response = await _dio.patch(host + api.url,
          queryParameters: queryParams, options: Options(headers: headers));
  }
  return response;
}

dynamic request(Api api,
    {Map<String, dynamic> queryParams,
    dynamic data,
    Map<String, dynamic> headers,
    Interceptor interceptor}) async {
  Response response = await _request(api,
      queryParams: queryParams,
      data: data,
      headers: headers,
      interceptor: interceptor);
  if (response == null || response.data == null) {
    return null;
  }
  return response.data;
}
